package com.lopo.mybatis_auto_insert;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReferenceUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONUtil;
import com.lopo.bean.TestBean;
import com.lopo.pagehelper.Response;
import com.lopo.util.JackSonUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Aspect
@Component
@Slf4j
public class ControllerAop {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("execution(* com.lopo.mybatis_auto_insert..*(..))")
    public void annotationLangCut() {
    }

    @Around("annotationLangCut()")
    @SneakyThrows
    public void around(JoinPoint point) {
        final Class<?> aClass = point.getTarget().getClass();
        final Object[] args = point.getArgs();

        Class[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
        String[] parameterNames = ((MethodSignature)  point.getSignature()).getParameterNames();
        for (int i = 0; i < parameterTypes.length; i++) {
            final Class parameterType = parameterTypes[i];
//            if (!parameterType.getName().contains("java.lang.String") && !parameterType.getName().contains("com.lopo")){
//                continue;
//            }
            final String parameterName = parameterNames[i];
            if (parameterType.getName().contains("java.lang.String") && parameterName.contains("dd")) {
                final Object arg = args[i];
                System.out.println("arg = " + arg);
            }

            if (parameterType.getName().contains("com.lopo")){
                final Object arg = args[i];
                final Map<String, Object> stringObjectMap = BeanUtil.beanToMap(arg);
                System.out.println("stringObjectMap = " + stringObjectMap);

            }

        }
        String methodName = point.getSignature().getName();
        final Method method = aClass.getMethod(methodName, parameterTypes);
        final HashMap<String, String> map = new HashMap<>();
        map.put("controllerName",aClass.getCanonicalName());
        map.put("args", JackSonUtils.toJson(args));
        map.put("methodName",method.getName());
        map.put("parameterTypes",JackSonUtils.toJson(parameterTypes));

        redisTemplate.opsForHash().putAll("sn", map);
    }
}
